import {defineStore} from 'pinia';
import i18n from "@/i18n/index.js";

export const useLanguageStore = defineStore('language', () => {
    const language = ref(import.meta.env.SSR ? 'zh' : localStorage.getItem('language') || 'zh');
    
    const getLanguage = computed(() => language.value);
    
    const setLanguage = (lang) => {
        i18n.global.locale.value = lang;
        language.value = lang;
        if (!import.meta.env.SSR) {
            localStorage.setItem('language', lang);
        }
    }
    
    setLanguage(language.value);
    
    return { getLanguage, setLanguage }
})
